﻿using LatteCoffee.coffeemake;
using LatteCoffee.coffeemodel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Windows.Documents;
using System.Windows.Forms;
using static LatteCoffee.coffeemodel.CoffeeMassageModel;

namespace LatteCoffee
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// IP,端口号的存储
        /// </summary>
        public string IPorPortUrl = Application.StartupPath + @"\IPorPortMessage.txt";
        public string coffeeMakeListsUrl = Application.StartupPath + @"\coffeeMakeLists.txt";
        public bool CupBol2 = true;
        public bool CupBol1 = true;

        public static string DYHImg = Application.StartupPath + @"\大运会.jpg";
        public static CoffeeMakeMsg coffeeMakeLists = new CoffeeMakeMsg();
        public static int DayCode = 0;
        /// <summary>
        /// 是否可以做咖啡
        /// </summary>
        public static bool isMaking = false;
        /// <summary>
        /// 订单是否可做
        /// </summary>
        public static bool isMake = true;
        /// <summary>
        /// 是否放了空杯子
        /// </summary>
        public static bool isCup = false;
        /// <summary>
        /// 是否出餐
        /// </summary>
        public static bool isCCup = false;
        /// <summary>
        /// 制作完成
        /// </summary>
        public static bool isCoffeeMake = false;
        public static bool isType = false;
        /// <summary>
        /// 其他东西的通讯返回数据
        /// </summary>
        private List<byte> buffer = new List<byte>();
        /// <summary>
        /// 是否可以打印
        /// </summary>
        public static bool isPrinter = false;
        public static bool isPrinterOver = false;
        /// <summary>
        /// 收到消息
        /// </summary>
        public static bool IsReviecePrinter = false;
        /// <summary>
        /// 错误信息
        /// </summary>
        public static string ErrorMsg = "";
        public static bool isCoffeeSocket = true;
        public static bool isPrinterSocket = false;
        public static bool isRobotSocket = false;
        public static string MakingType = "ready";
        Thread coffee_thr = null;
        Thread robot_thr = null;
        Thread ElseSerial_thr = null;
        Thread DiaitalBoolMsg_thr = null;
        Thread printer_thr = null;
        Thread All_thr = null;
        Thread CoffeeMakeList_thr = null;
        public Thread EWMthr = null;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                // var ddd = Form1.SetImg(Form1.DYHImg);
                // string json = JsonConvert.SerializeObject(new MsgSocket());
                if (!File.Exists(IPorPortUrl))
                {
                    using (FileStream stream = File.Create(IPorPortUrl))
                    {
                        stream.Close();
                        stream.Dispose();
                        IPorPortInfo porPortInfo1 = IPorPortInfo.Instance;
                        File.WriteAllText(IPorPortUrl, JsonConvert.SerializeObject(porPortInfo1));
                    }
                }
                else
                {
                    var dd = File.ReadAllText(IPorPortUrl);
                    var porPortInfo = JsonConvert.DeserializeObject<IPorPortInfo>(File.ReadAllText(IPorPortUrl));
                    IPorPortInfo.Instance.CoffeeIP = porPortInfo.CoffeeIP;
                    IPorPortInfo.Instance.CoffeePort = porPortInfo.CoffeePort;
                    IPorPortInfo.Instance.RobotIPLeft = porPortInfo.RobotIPLeft;
                    IPorPortInfo.Instance.RobotIPRight = porPortInfo.RobotIPRight;
                    IPorPortInfo.Instance.PortName = porPortInfo.PortName;
                    IPorPortInfo.Instance.PrinterCoffeeIP = porPortInfo.PrinterCoffeeIP;
                    IPorPortInfo.Instance.PrinterCofferPort = porPortInfo.PrinterCofferPort;
                }
                Thread thr = new Thread(SocketClients.Instance.socketclient);
                thr.IsBackground = true;
                thr.Start();
                printer_thr = new Thread(Printer_Thr);
                printer_thr.IsBackground = true;
                printer_thr.Start();
                ElseSerial_thr = new Thread(ElseSerial_Thr);
                ElseSerial_thr.IsBackground = true;
                ElseSerial_thr.Start();
                coffee_thr = new Thread(IsMakingTrue);
                coffee_thr.IsBackground = true;
                coffee_thr.Start();
                // CoffeeMake();
                EWMthr = new Thread(EWMMsg.Instance.IOSerial_Thr);//二维码
                EWMthr.IsBackground = true;
                EWMthr.Start();
                comboBox1.SelectedIndex = 0;
            }
            catch (Exception)
            {
                return;
            }
        }

        public void Coffee_Thr()
        {
            try
            {
                if (!PingIP(IPorPortInfo.Instance.CoffeeIP))
                {
                    Thread.Sleep(100);
                    Coffee_Thr();
                }
                websocket();
            }
            catch (Exception)
            {
                wSocketClient.Close();
                wSocketClient.OnOpen -= WSocketClient_OnOpen;
                wSocketClient.OnMessage -= WSocketClient_OnMessage;
                wSocketClient.OnClose -= WSocketClient_OnClose;
                wSocketClient.OnError -= WSocketClient_OnError;
                Thread.Sleep(100);
                Coffee_Thr();
            }
        }
        public void Robot_Thr()
        {
            try
            {
                //if (!PingIP(IPorPortInfo.Instance.RobotIPLeft) || !PingIP(IPorPortInfo.Instance.RobotIPRight))
                //{
                //    Thread.Sleep(100);
                //    Robot_Thr();
                //}
                RobotOperate.Read_CSV(); //读取轨迹文件
                RobotOperate.Robot_Initialisation_double(); //机器人初始化
                isRobotSocket = true;
            }
            catch (Exception ex)
            {
                Thread.Sleep(100);
                Robot_Thr();
            }
        }
        public void ElseSerial_Thr()
        {
            try
            {
                var ddd = SerialPort.GetPortNames().ToList();
                if (!SerialPort.GetPortNames().ToList().Any(a => a.ToLower().Contains(IPorPortInfo.Instance.PortName)))
                {
                    Thread.Sleep(1000);
                    ElseSerial_Thr();
                }
                ElseSerialPort.Instance.IceMachineConnect(IPorPortInfo.Instance.PortName, 38400, "", 8, 1);//其他串口通讯
                ElseSerialPort.Instance.serialPort.DataReceived += SerialPort_DataReceived;
                AllBol = false;
                Thread.Sleep(100);
                RobotBol = false;
                AllBol = true;
                Robot_Thr();
                All_thr = new Thread(AllMessages);
                All_thr.IsBackground = true;
                All_thr.Start();
                CoffeeMakeList_thr = new Thread(MessageShow);
                CoffeeMakeList_thr.IsBackground = true;
                CoffeeMakeList_thr.Start();

            }
            catch (Exception)
            {
                ElseSerialPort.Instance.serialPort.DataReceived -= SerialPort_DataReceived;
                Thread.Sleep(100);
                ElseSerial_Thr();
            }
        }
        public void Printer_Thr()
        {
            try
            {
                if (!PingIP(IPorPortInfo.Instance.PrinterCoffeeIP))
                {
                    Thread.Sleep(100);
                    Printer_Thr();
                    return;
                }
                Pwebsocket();
                //  Heart();
            }
            catch (Exception)
            {
                Printer_Thr();
            }
        }
        /// <summary>
        /// 确定所有机器都已就为再往下进行
        /// </summary>
        public void SocketAndSerial()
        {
            try
            {
                if (!PingIP(IPorPortInfo.Instance.CoffeeIP) || !PingIP(IPorPortInfo.Instance.RobotIPLeft) || !PingIP(IPorPortInfo.Instance.RobotIPRight) || !PingIP(IPorPortInfo.Instance.PrinterCoffeeIP) || !SerialPort.GetPortNames().ToList().Any(a => a.Contains(IPorPortInfo.Instance.PortName)))
                {
                    SocketAndSerial();
                    //return;
                }
            }
            catch (Exception ex)
            {

                return;
            }
        }
        #region 其他通讯接收信息
        bool cupbol1 = false;
        bool cupbol2 = false;
        public static bool RobotBol = false;
        public void CupMsg1()
        {
            try
            {
                this.BeginInvoke(new Action(() =>
                {
                    Console.WriteLine("OK1");
                    Console.WriteLine("1");
                    Console.WriteLine("112");
                    Console.WriteLine("12");
                    Console.WriteLine(DiaitalBool.Instance.CupFeed1_Sel_Bol);
                    if (!DiaitalBool.Instance.CupFeed1_Sel_Bol)
                    {
                        Console.WriteLine("16");
                        if (DiaitalBool.Instance.MotorUP1_Sel_Bol)
                        {
                            Console.WriteLine("17");
                            Form1.AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction1_start);
                            Console.WriteLine("yes3");
                            Thread.Sleep(100);
                            Form1.AllBol = true;

                            Thread.Sleep(100);
                            Console.WriteLine("19");
                            while (true)
                            {
                                Console.WriteLine("20");
                                if (DiaitalBool.Instance.MotorOriginalPoint1_Sel_Bol)
                                {
                                    Console.WriteLine("20");
                                    DiaitalBool.Instance.UPCupBtn1_Sel_Bol = false;

                                    break;
                                }
                                else
                                {
                                    Thread.Sleep(100);
                                }
                                Thread.Sleep(10);
                            }
                            Console.WriteLine("20");
                            Form1.AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction1_end);
                            Console.WriteLine("yes3");
                            Thread.Sleep(100);
                            Form1.AllBol = true;
                            cupbol1 = false;
                        }
                        else
                        {
                            if (!cupbol1)
                            {
                                Form1.AllBol = false;
                                Thread.Sleep(100);
                                ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Impulse1_start);
                                Console.WriteLine("yes3");
                                Thread.Sleep(100);
                                Form1.AllBol = true;
                                while (true)
                                {
                                    Console.WriteLine("22");
                                    if (DiaitalBool.Instance.MotorUP1_Sel_Bol)
                                    {
                                        cupbol1 = true;
                                        break;
                                    }
                                    else if (DiaitalBool.Instance.CupFeed1_Sel_Bol)
                                    {
                                        Thread.Sleep(100);
                                        break;
                                    }
                                    else
                                    {
                                        Thread.Sleep(100);
                                    }
                                    Thread.Sleep(10);
                                }
                                Form1.AllBol = false;
                                Thread.Sleep(100);
                                ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Impulse1_end);
                                Console.WriteLine("yes3");
                                Thread.Sleep(100);

                            }
                            if (cupbol1 && !DiaitalBool.Instance.CupFeed1_Sel_Bol)
                            {
                                if (!DiaitalBool.Instance.MotorOriginalPoint1_Sel_Bol)
                                {
                                    Console.WriteLine("17");
                                    Form1.AllBol = false;
                                    Thread.Sleep(100);
                                    ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction1_start);
                                    Console.WriteLine("yes3");
                                    Thread.Sleep(100);
                                    Form1.AllBol = true;

                                    Thread.Sleep(1000);
                                    Console.WriteLine("19");
                                    while (true)
                                    {
                                        Console.WriteLine("20");
                                        if (DiaitalBool.Instance.MotorOriginalPoint1_Sel_Bol)
                                        {
                                            Console.WriteLine("20");
                                            DiaitalBool.Instance.UPCupBtn1_Sel_Bol = false;
                                            break;
                                        }
                                        else
                                        {
                                            Thread.Sleep(100);
                                        }
                                        Thread.Sleep(10);
                                    }
                                    Console.WriteLine("20");
                                    Form1.AllBol = false;
                                    Thread.Sleep(100);
                                    ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction1_end);
                                    Console.WriteLine("yes3");
                                    Thread.Sleep(100);
                                    Form1.AllBol = true;
                                }
                                cupbol1 = false;
                            }
                            Form1.AllBol = true;

                            Thread.Sleep(1000);
                        }
                    }
                    Console.WriteLine("完成0");
                    CupBol1 = true;

                }));
                //Thread.Sleep(1000);
                //CupMsg1();
            }
            catch (Exception)
            {
                Thread.Sleep(1000);
                CupMsg1();
                //thread_Cup1.Abort();
                //thread_Cup1.Start();
                return;
            }
        }
        public static bool AllBol = true;
        public void AllMessages()
        {
            try
            {
                while (true)
                {
                    if (AllBol)
                        ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.AllMessage);
                    Thread.Sleep(100);
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        int i = 0;
        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                // var  ddd=serialPort.BytesToRead
                //读取串口缓冲区的字节数据

                byte[] result = new byte[ElseSerialPort.Instance.serialPort.BytesToRead];
                ElseSerialPort.Instance.serialPort.Read(result, 0, ElseSerialPort.Instance.serialPort.BytesToRead);
                buffer.AddRange(result);
                string str = "";// $"{DateTime.Now}：\n";
                                //判断读写模式
                if (buffer.Count > 0x03)
                    str += $"{Encoding.ASCII.GetString(result)}";
                for (int i = 0; i < result.Length; i++)
                {
                    str += $"{result[i].ToString("X2")} ";
                }
                // Console.WriteLine(str);
                MsgShow(str);
                Thread.Sleep(1);
            }
            catch (Exception ex)
            {
                return;
            }
        }
        public static bool isWLMsg = true;
        public int IsCoffee = 0;
        public void DigitalMsgBol2(string str)
        {
            try
            {
                var Bol = str.Split(' ');
                bool bol = false;
                if (DiaitalBool.Instance.OutMealCup_Sel_Bol != (Bol[4] == "01" ? true : false))
                {
                    WuLiaoCoffeeSel.Instance.IsChucan = (Bol[4] == "01" ? true : false);
                    bol = true;
                }
                if (isCCup)
                {
                    if (!(Bol[4] == "01" ? true : false))
                    {
                        if (IsCoffee >= 10)
                        {
                            isCCup = false;
                            IsCoffee = 0;
                            Console.WriteLine("nazoyl");
                        }
                        else
                        {
                            Console.WriteLine("nazoyl  " + IsCoffee);
                            IsCoffee++;
                        }
                    }
                    else
                    {
                        IsCoffee = 0;
                    }
                }
                else
                {
                    Form1.isCup = Bol[4] == "01" ? true : false;
                }
                if (DiaitalBool.Instance.OutMealCup_Sel_Bol != (Bol[4] == "01" ? true : false))
                {
                    WuLiaoCoffeeSel.Instance.IsChucan = (Bol[4] == "01" ? true : false);
                    bol = true;
                }
                DiaitalBool.Instance.OutMealCup_Sel_Bol = Bol[4] == "01" ? true : false;

                Console.WriteLine(DateTime.Now.ToString() + " " + DiaitalBool.Instance.OutMealCup_Sel_Bol);
                if (DiaitalBool.Instance.Water_Sel_Bol != (Bol[6] == "01" ? true : false))
                {
                    WuLiaoCoffeeSel.Instance.IsWater = (Bol[6] == "01" ? true : false);
                    bol = true;
                }
                DiaitalBool.Instance.Water_Sel_Bol = Bol[6] == "01" ? true : false;
                if (DiaitalBool.Instance.Milk_Sel_Bol != (Bol[8] == "01" ? true : false))
                {
                    WuLiaoCoffeeSel.Instance.IsMilk = (Bol[8] == "01" ? true : false);
                    bol = true;
                }
                DiaitalBool.Instance.Milk_Sel_Bol = Bol[8] == "01" ? true : false;
                IsMsgError = bol;
                if (bol)
                {
                    ClientList clientList = new ClientList();
                    clientList.MsgBol = WuLiaoCoffeeSel.Instance;
                    clientList.code = 2;
                    SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        string nostrs = "";
        public void MsgShow(string str)
        {
            try
            {
                // Console.WriteLine(str);
                int count = str.Length / 315;
                if (count > 0)
                {
                    var str1 = str.Substring(str.Length - count * 315, 315);
                    //Console.WriteLine(str1);
                    if (str1.Split(' ').Count() == 105)
                    {
                        DigitalMsgBol2(str1);
                        if (count > 1)
                            MsgShow(str.Substring(str.Length - (count - 1) * 315, (count - 1) * 315));
                    }
                    else
                    {
                        var nostr = str1.Split(' ');
                        int cc = int.TryParse(nostr[0].Substring(nostr[0].Length - 2, 2), out int a) ? a : -1;
                        if (cc != -1)
                        {
                            nostrs += nostr[0].Substring(nostr[0].Length - 2, 2) + " ";
                            for (int i = 1; i < nostr.Count() - 1; i++)
                            {
                                nostrs += nostr[i] + " ";
                            }
                            // Console.WriteLine(nostrs);
                            if (nostrs.Substring(0, 5) == "02 04")
                            {
                                if (nostrs.Count() >= 315)
                                {
                                    DigitalMsgBol2(nostrs);
                                    nostrs = "";
                                }
                            }
                            else
                            {
                                nostrs = "";
                            }
                        }
                    }
                }
                else
                {

                    var nostr = str.Split(' ');
                    nostrs += nostr[0].Substring(nostr[0].Length - 2, 2) + " ";
                    for (int i = 1; i < nostr.Count() - 1; i++)
                    {
                        nostrs += nostr[i] + " ";
                    }
                    // Console.WriteLine(nostrs);
                    if (nostrs.Substring(0, 5) == "02 04")
                    {
                        if (nostrs.Count() >= 315)
                        {
                            DigitalMsgBol2(nostrs);
                            nostrs = "";
                        }
                    }
                    else
                    {
                        if (nostrs.Substring(0, 5) == "01 05")
                        {
                            Console.WriteLine(str);
                        }
                        else if (nostrs.Substring(0, 5) == "03 02")
                        {
                            Console.WriteLine(nostrs);
                            var robstr = nostrs.Split(' ');
                            RobotBol = robstr[3] == "01" ? true : false;
                            Console.WriteLine(robstr[3] == "01" ? true : false);
                        }
                        else if (nostrs.Substring(0, 5) == "04 02")
                        {
                            Console.WriteLine(nostrs);
                            var robstr = nostrs.Split(' ');
                            RobotBol = robstr[3] == "01" ? true : false;
                            Console.WriteLine(robstr[3] == "01" ? true : false);
                        }
                        nostrs = "";
                    }
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        #endregion
        #region socket服务器
        public static bool status_coffee = true;
        private static byte[] result = new byte[1024];
        private static int myProt = 8885;  //端口 
        static Socket serverSocket;
        public static Socket Socket_Sreen = null;
        public static Socket clientSocketes;
        public static bool IsMsgError = true;
        public static List<SocketListNew> clientSocketsList = new List<SocketListNew>();
        public static bool containerCoffeeGroundsBol = false;

        /// <summary>
        /// 服务器启动监听各种信息动态
        /// </summary>
        public void CoffeeMake()
        {
            try
            {
                string LocalIP = "192.168.0.77";
                var host = Dns.GetHostEntry(Dns.GetHostName());
                foreach (var ips in host.AddressList)
                {
                    if (ips.AddressFamily == AddressFamily.InterNetwork)
                    {
                        LocalIP = ips.ToString();
                        // Console.WriteLine"IP Address = " + (ips.ToString());
                    }
                }
                //服务器IP地址 
                IPAddress ip = IPAddress.Parse(LocalIP);
                serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                serverSocket.Bind(new IPEndPoint(ip, myProt)); //绑定IP地址：端口 
                serverSocket.Listen(10);  //设定最多10个排队连接请求 
                Console.WriteLine("启动监听{0}成功", serverSocket.LocalEndPoint.ToString());
                //通过Clientsoket发送数据 
                Thread myThread = new Thread(ListenClientConnect);
                myThread.IsBackground = true;
                myThread.Start();
                coffee_thr = new Thread(IsMakingTrue);
                coffee_thr.IsBackground = true;
                coffee_thr.Start();

            }
            catch (Exception)
            {
                serverSocket.Shutdown(SocketShutdown.Both);
                serverSocket.Close();
                CoffeeMake();
                return;
            }
        }
        //public void CoffeeMakeListNow()
        //{
        //    try
        //    {
        //        while (true)
        //        {
        //            try
        //            {
        //                if (coffeeMakeLists != null)
        //                {
        //                    if (coffeeMakeLists.lists.Count > 0)
        //                    {
        //                        if (coffeeMakeLists.lists[0].MakingType == "waiting")
        //                        {
        //                            Thread.Sleep(1000);
        //                            CoffeeMaking(coffeeMakeLists.lists[0].MakingID,make);
        //                        }
        //                    }
        //                }
        //                Thread.Sleep(1000);
        //            }
        //            catch (Exception)
        //            {
        //                Thread.Sleep(1000);
        //            }
        //        }
        //    }
        //    catch (Exception)
        //    {
        //        return;
        //    }
        //}
        public void IsMakingTrue()
        {
            try
            {
                while (true)
                {
                    isMaking = CoffeeMakes.Instance.IsMakingBol();
                    Thread.Sleep(1000);
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        Thread receiveThread;
        /// <summary> 
        /// 监听客户端连接 
        /// </summary> 
        private void ListenClientConnect()
        {
            try
            {
                while (true)
                {
                    Socket clientSocket = serverSocket.Accept();
                    string ip = ((IPEndPoint)clientSocket.RemoteEndPoint).Address.ToString();
                    Console.WriteLine("点单");
                    var msg = MsgSockets((DiaitalBool.Instance.MotorOriginalPoint1_Sel_Bol && DiaitalBool.Instance.MotorOriginalPoint2_Sel_Bol), !DiaitalBool.Instance.CoffeeBean_Sel_Bol, !DiaitalBool.Instance.ChocolatePowder_Sel_Bol, !DiaitalBool.Instance.Milk_Sel_Bol, !DiaitalBool.Instance.Water_Sel_Bol, !DiaitalBool.Instance.Fructose1_Sel_Bol, !DiaitalBool.Instance.Fructose2_Sel_Bol, !DiaitalBool.Instance.Fructose3_Sel_Bol, isCoffeeSocket, isPrinterSocket, isRobotSocket, "ready");
                    Console.WriteLine(JsonConvert.SerializeObject(msg));
                    clientSocket.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(msg)));
                    Thread receiveThread = new Thread(ReceiveMessage);
                    receiveThread.IsBackground = true;
                    receiveThread.Start(clientSocket);
                    SocketListNew socketListNew = new SocketListNew();
                    socketListNew.IP = ip;
                    socketListNew.socket1 = clientSocket;
                    socketListNew.thread = receiveThread;
                    clientSocketsList.Add(socketListNew);
                    Thread.Sleep(10);
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        public void xunhuan()
        {
            try
            {
                while (true)
                {

                    if (IsMsgError)
                    {
                        try
                        {
                            IsMsgError = false;
                            if (Socket_Sreen != null)
                            {
                                Msg msg1 = new Msg();
                                msg1.diaital = DiaitalBool.Instance;
                                msg1.msgBol = DigitalMsgBol.Instance;
                                Coffeemake coffeemake = new Coffeemake();
                                coffeemake.MsgBool = true;
                                coffeemake.MsgSreen = JsonConvert.SerializeObject(msg1);
                                coffeemake.MakingID = "sreen";
                                Socket_Sreen.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(coffeemake)));
                            }
                            Thread.Sleep(100);
                        }
                        catch (Exception)
                        {
                            Thread.Sleep(100);
                        }
                    }
                    Thread.Sleep(100);
                }
            }
            catch (Exception)
            {
                xunhuan();
            }
        }
        /// <summary>
        /// 发送信息
        /// </summary>
        /// <param name="MotorOriginalPoint_Bol">杯</param>
        /// <param name="CoffeeBean_Bol">咖啡豆</param>
        /// <param name="ChocolatePowder_Bol">巧克力粉</param>
        /// <param name="Milk_Bol">奶</param>
        /// <param name="Water_Bol">水</param>
        /// <param name="Fructose1_Bol">果糖1</param>
        /// <param name="Fructose2_Bol">果糖2</param>
        /// <param name="Fructose3_Bol">果糖3</param>
        /// <param name="CoffeeSocket_Bol">咖啡机连接</param>
        /// <param name="PrinterSocket_Bol">打印机连接</param>
        /// <param name="RobotSocket_Bol">机械臂连接</param>
        /// <param name="type">机器状态</param>
        /// <returns></returns>
        public MsgSocket MsgSockets(bool MotorOriginalPoint_Bol, bool CoffeeBean_Bol, bool ChocolatePowder_Bol, bool Milk_Bol, bool Water_Bol, bool Fructose1_Bol, bool Fructose2_Bol, bool Fructose3_Bol, bool CoffeeSocket_Bol, bool PrinterSocket_Bol, bool RobotSocket_Bol, string type)
        {
            try
            {
                MsgSocket msg = new MsgSocket();
                ErrorMsg errorMsg = new ErrorMsg();
                SocketMessage socketMessage = new SocketMessage();
                errorMsg.MotorOriginalPoint_Bol = MotorOriginalPoint_Bol;
                errorMsg.CoffeeBean_Bol = CoffeeBean_Bol;
                errorMsg.ChocolatePowder_Bol = ChocolatePowder_Bol;
                errorMsg.Milk_Bol = Milk_Bol;
                errorMsg.Water_Bol = Water_Bol;
                errorMsg.Fructose1_Bol = Fructose1_Bol;
                errorMsg.Fructose2_Bol = Fructose2_Bol;
                errorMsg.Fructose3_Bol = Fructose3_Bol;
                socketMessage.RobotSocket_Bol = RobotSocket_Bol;
                socketMessage.CoffeeSocket_Bol = CoffeeSocket_Bol;
                socketMessage.PrinterSocket_Bol = PrinterSocket_Bol;
                msg.Socketmessage = socketMessage;
                msg.ErrorMessage = errorMsg;
                msg.Type = type;
                return msg;
            }
            catch (Exception)
            {
                return null;
            }
        }
        public bool MsgType(bool MotorOriginalPoint_Bol, bool CoffeeBean_Bol, bool ChocolatePowder_Bol, bool Milk_Bol, bool Water_Bol, bool Fructose1_Bol, bool Fructose2_Bol, bool Fructose3_Bol, bool CoffeeSocket_Bol, bool PrinterSocket_Bol, bool RobotSocket_Bol, string type)
        {
            try
            {
                bool bol = !MotorOriginalPoint_Bol && !CoffeeBean_Bol && !ChocolatePowder_Bol && !Milk_Bol && !Water_Bol && !Fructose1_Bol && !Fructose2_Bol && !Fructose3_Bol && RobotSocket_Bol && CoffeeSocket_Bol && PrinterSocket_Bol;
                return bol;
            }
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary> 
        /// 接收消息 
        /// </summary> 
        /// <param name="clientSocket"></param> 
        private void ReceiveMessage(object clientSockets)
        {
            Socket myClientSocket = (Socket)clientSockets;

            while (true)
            {
                try
                {
                    //通过clientSocket接收数据 
                    int receiveNumber = myClientSocket.Receive(result);
                    if (receiveNumber == 0) continue;
                    try
                    {
                        var msg = Encoding.UTF8.GetString(result, 0, receiveNumber);
                        if (msg.Trim().Length != 0)
                        {
                            var making = JsonConvert.DeserializeObject<Coffeemake>(msg);
                            if (making != null)
                            {
                                if (making.MakingID == "sreen")
                                {
                                    if (making.type == 0)
                                    {
                                        // clientSocketsList.Remove(myClientSocket);
                                        Socket_Sreen = myClientSocket;
                                        Msg msg1 = new Msg();
                                        msg1.diaital = DiaitalBool.Instance;
                                        msg1.msgBol = DigitalMsgBol.Instance;
                                        Coffeemake coffeemake = new Coffeemake();
                                        coffeemake.MsgBool = true;
                                        coffeemake.MsgSreen = JsonConvert.SerializeObject(msg1);
                                        coffeemake.MakingID = "sreen";
                                        myClientSocket.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(coffeemake)));
                                    }
                                    else
                                    {
                                        MessageSreen(myClientSocket, making.MsgSreen, making.MsgBool);
                                    }
                                }
                                else
                                {
                                    if (coffeeMakeLists.lists.Count == 5)
                                    {
                                        var msgs = MsgSockets((DiaitalBool.Instance.MotorOriginalPoint1_Sel_Bol && DiaitalBool.Instance.MotorOriginalPoint2_Sel_Bol), !DiaitalBool.Instance.CoffeeBean_Sel_Bol, !DiaitalBool.Instance.ChocolatePowder_Sel_Bol, !DiaitalBool.Instance.Milk_Sel_Bol, !DiaitalBool.Instance.Water_Sel_Bol, !DiaitalBool.Instance.Fructose1_Sel_Bol, !DiaitalBool.Instance.Fructose2_Sel_Bol, !DiaitalBool.Instance.Fructose3_Sel_Bol, isCoffeeSocket, isPrinterSocket, isRobotSocket, "busy");
                                        myClientSocket.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(msgs)));
                                        CoffeeType type = new CoffeeType();
                                        type.MakingType = false;//MsgType((DiaitalBool.Instance.MotorOriginalPoint1_Sel_Bol && DiaitalBool.Instance.MotorOriginalPoint2_Sel_Bol), !DiaitalBool.Instance.CoffeeBean_Sel_Bol, !DiaitalBool.Instance.ChocolatePowder_Sel_Bol, !DiaitalBool.Instance.Milk_Sel_Bol, !DiaitalBool.Instance.Water_Sel_Bol, !DiaitalBool.Instance.Fructose1_Sel_Bol, !DiaitalBool.Instance.Fructose2_Sel_Bol, !DiaitalBool.Instance.Fructose3_Sel_Bol, isCoffeeSocket, isPrinterSocket, isRobotSocket, "ready");
                                        Console.WriteLine(type.MakingType);
                                        myClientSocket.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(type)));
                                    }
                                    else
                                    {

                                        CoffeeType type = new CoffeeType();
                                        if (!containerCoffeeGroundsBol)
                                        {
                                            //CoffeeType type = new CoffeeType();
                                            type.MakingType = false;//MsgType((DiaitalBool.Instance.MotorOriginalPoint1_Sel_Bol && DiaitalBool.Instance.MotorOriginalPoint2_Sel_Bol), !DiaitalBool.Instance.CoffeeBean_Sel_Bol, !DiaitalBool.Instance.ChocolatePowder_Sel_Bol, !DiaitalBool.Instance.Milk_Sel_Bol, !DiaitalBool.Instance.Water_Sel_Bol, !DiaitalBool.Instance.Fructose1_Sel_Bol, !DiaitalBool.Instance.Fructose2_Sel_Bol, !DiaitalBool.Instance.Fructose3_Sel_Bol, isCoffeeSocket, isPrinterSocket, isRobotSocket, "ready");
                                                                    // Console.WriteLine(type.MakingType);
                                                                    // myClientSocket.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(type)));
                                        }
                                        else
                                            type.MakingType = MsgType((DiaitalBool.Instance.MotorOriginalPoint1_Sel_Bol && DiaitalBool.Instance.MotorOriginalPoint2_Sel_Bol), !DiaitalBool.Instance.CoffeeBean_Sel_Bol, !DiaitalBool.Instance.ChocolatePowder_Sel_Bol, !DiaitalBool.Instance.Milk_Sel_Bol, !DiaitalBool.Instance.Water_Sel_Bol, !DiaitalBool.Instance.Fructose1_Sel_Bol, !DiaitalBool.Instance.Fructose2_Sel_Bol, !DiaitalBool.Instance.Fructose3_Sel_Bol, isCoffeeSocket, isPrinterSocket, isRobotSocket, "ready");
                                        Console.WriteLine(type.MakingType);
                                        myClientSocket.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(type)));
                                        if (type.MakingType)
                                        {
                                            var msgs = MsgSockets((DiaitalBool.Instance.MotorOriginalPoint1_Sel_Bol && DiaitalBool.Instance.MotorOriginalPoint2_Sel_Bol), !DiaitalBool.Instance.CoffeeBean_Sel_Bol, !DiaitalBool.Instance.ChocolatePowder_Sel_Bol, !DiaitalBool.Instance.Milk_Sel_Bol, !DiaitalBool.Instance.Water_Sel_Bol, !DiaitalBool.Instance.Fructose1_Sel_Bol, !DiaitalBool.Instance.Fructose2_Sel_Bol, !DiaitalBool.Instance.Fructose3_Sel_Bol, isCoffeeSocket, isPrinterSocket, isRobotSocket, "ready");
                                            myClientSocket.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(msgs)));
                                            //var msg = MsgSockets((DiaitalBool.Instance.MotorOriginalPoint1_Sel_Bol && DiaitalBool.Instance.MotorOriginalPoint2_Sel_Bol), !DiaitalBool.Instance.CoffeeBean_Sel_Bol, !DiaitalBool.Instance.ChocolatePowder_Sel_Bol, !DiaitalBool.Instance.Milk_Sel_Bol, !DiaitalBool.Instance.Water_Sel_Bol, !DiaitalBool.Instance.Fructose1_Sel_Bol, !DiaitalBool.Instance.Fructose2_Sel_Bol, !DiaitalBool.Instance.Fructose3_Sel_Bol, isCoffeeSocket, isPrinterSocket, isRobotSocket, "busy");
                                            //foreach (var item in clientSocketsList)
                                            //{
                                            //    item.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(msg)));
                                            //}
                                            //CoffeeMaking(making.MakingID);
                                            clientSocketes = myClientSocket;
                                            CoffeeMakingList(making.MakingID);
                                        }
                                    }
                                }
                            }
                        }
                        Thread.Sleep(10);
                    }
                    catch (Exception)
                    {
                        // clientSocketsList.Remove(myClientSocket);
                        //myClientSocket.Shutdown(SocketShutdown.Both);
                        //myClientSocket.Close();
                        //myClientSocket.Dispose();
                        //break;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    if (myClientSocket == Socket_Sreen)
                        Socket_Sreen = null;
                    // clientSocketsList.Remove(myClientSocket);
                    //  myClientSocket.Shutdown(SocketShutdown.Both);
                    // myClientSocket.Close();
                    // myClientSocket.Dispose();
                    break;
                }
            }

        }

        public void MessageSreen(Socket socket, string msg, bool MsgBol, int typedex = 1)
        {
            try
            {
                Coffeemake coffeemake = new Coffeemake();
                coffeemake.MsgBool = MsgBol;
                coffeemake.MsgSreen = msg;
                coffeemake.MakingID = "sreen";
                coffeemake.type = typedex;
                socket.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(coffeemake)));
                switch (msg)
                {
                    case "BSG_Impulse1":
                        if (MsgBol)
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Impulse1_start);
                            DigitalMsgBol.Instance.BSG_Impulse1_Bol = true;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        else
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Impulse1_end);
                            DigitalMsgBol.Instance.BSG_Impulse1_Bol = false;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        break;
                    case "BSG_Direction1":
                        if (MsgBol)
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction1_start);
                            DigitalMsgBol.Instance.BSG_Direction1_Bol = true;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        else
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction1_end);
                            DigitalMsgBol.Instance.BSG_Direction1_Bol = false;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        break;
                    case "BSG_Impulse2":
                        if (MsgBol)
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Impulse2_start);
                            DigitalMsgBol.Instance.BSG_Impulse2_Bol = true;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        else
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Impulse2_end);
                            DigitalMsgBol.Instance.BSG_Impulse2_Bol = false;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        break;
                    case "BSG_Direction2":
                        if (MsgBol)
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction2_start);
                            DigitalMsgBol.Instance.BSG_Direction2_Bol = true;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        else
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction2_end);
                            DigitalMsgBol.Instance.BSG_Direction2_Bol = false;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        break;
                    case "PushRod_UP":
                        if (MsgBol)
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_start);
                            DigitalMsgBol.Instance.PushRod_UP_Bol = true;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        else
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
                            DigitalMsgBol.Instance.PushRod_UP_Bol = false;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        break;
                    case "PushRod_Down":
                        if (MsgBol)
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_start);
                            DigitalMsgBol.Instance.PushRod_Down_Bol = true;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        else
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_end);
                            DigitalMsgBol.Instance.PushRod_Down_Bol = false;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        break;
                    case "OutMealDoor_UP":
                        if (MsgBol)
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.OutMealDoor_UP_start);
                            DigitalMsgBol.Instance.OutMealDoor_UP_Bol = true;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        else
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.OutMealDoor_UP_end);
                            DigitalMsgBol.Instance.OutMealDoor_UP_Bol = false;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        break;
                    case "OutMealDoor_Down":
                        if (MsgBol)
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.OutMealDoor_Down_start);
                            DigitalMsgBol.Instance.OutMealDoor_Down_Bol = true;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        else
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.OutMealDoor_Down_end);
                            DigitalMsgBol.Instance.OutMealDoor_Down_Bol = false;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        break;

                    case "RobotPowerOn1":
                        if (MsgBol)
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.RobotPowerOn1_start);
                            DigitalMsgBol.Instance.RobotPowerOn1_Bol = true;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        else
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.RobotPowerOn1_end);
                            DigitalMsgBol.Instance.RobotPowerOn1_Bol = false;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        break;
                    case "RobotPowerOn2":
                        if (MsgBol)
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.RobotPowerOn2_start);
                            DigitalMsgBol.Instance.RobotPowerOn2_Bol = true;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        else
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.RobotPowerOn2_end);
                            DigitalMsgBol.Instance.RobotPowerOn2_Bol = false;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        break;
                    case "CoolingFan":
                        if (MsgBol)
                        {
                            //ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.CoolingFan_start);
                            //DigitalMsgBol.Instance.CoolingFan_Bol = true;
                        }
                        else
                        {
                            //ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.CoolingFan_end);
                            //DigitalMsgBol.Instance.CoolingFan_Bol = false;
                        }
                        break;
                    case "WashCupSolenoidValve":
                        if (MsgBol)
                        {
                            //ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.WashCupSolenoidValve_start);
                            //DigitalMsgBol.Instance.WashCupSolenoidValve_Bol = true;
                        }
                        else
                        {
                            //ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.WashCupSolenoidValve_end);
                            //DigitalMsgBol.Instance.WashCupSolenoidValve_Bol = false;
                        }
                        break;
                    case "CupWashingPump":
                        if (MsgBol)
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.CupWashingPump_start);
                            DigitalMsgBol.Instance.CupWashingPump_Bol = true;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        else
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.CupWashingPump_end);
                            DigitalMsgBol.Instance.CupWashingPump_Bol = false;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        break;
                    case "OverheadLight1":
                        if (MsgBol)
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.OverheadLight1_start);
                            DigitalMsgBol.Instance.OverheadLight1_Bol = true;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        else
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.OverheadLight1_end);
                            DigitalMsgBol.Instance.OverheadLight1_Bol = false;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        break;
                    case "OverheadLight2":
                        if (MsgBol)
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.OverheadLight2_start);
                            DigitalMsgBol.Instance.OverheadLight2_Bol = true;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        else
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.OverheadLight2_end);
                            DigitalMsgBol.Instance.OverheadLight2_Bol = false;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        break;
                    case "OverheadLight3":
                        if (MsgBol)
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.OverheadLight3_start);
                            DigitalMsgBol.Instance.OverheadLight3_Bol = true;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        else
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.OverheadLight3_end);
                            DigitalMsgBol.Instance.OverheadLight3_Bol = false;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        break;
                    case "OverheadLight4":
                        if (MsgBol)
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.OverheadLight4_start);
                            DigitalMsgBol.Instance.OverheadLight4_Bol = true;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        else
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.OverheadLight4_end);
                            DigitalMsgBol.Instance.OverheadLight4_Bol = false;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        break;
                    case "OverheadLight5":
                        if (MsgBol)
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.OverheadLight5_start);
                            DigitalMsgBol.Instance.OverheadLight5_Bol = true;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        else
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.OverheadLight5_end);
                            DigitalMsgBol.Instance.OverheadLight5_Bol = false;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        break;
                    case "LightStrip1":
                        if (MsgBol)
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LightStrip1_start);
                            DigitalMsgBol.Instance.LightStrip1_Bol = true;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        else
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LightStrip1_end);
                            DigitalMsgBol.Instance.LightStrip1_Bol = false;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        break;
                    case "LightStrip2":
                        if (MsgBol)
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LightStrip2_start);
                            DigitalMsgBol.Instance.LightStrip2_Bol = true;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        else
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LightStrip2_end);
                            DigitalMsgBol.Instance.LightStrip2_Bol = false;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        break;
                    case "LightStrip3":
                        if (MsgBol)
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LightStrip3_start);
                            DigitalMsgBol.Instance.LightStrip3_Bol = true;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        else
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LightStrip3_end);
                            DigitalMsgBol.Instance.LightStrip3_Bol = false;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        break;
                    case "LightStrip4":
                        if (MsgBol)
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LightStrip4_start);
                            DigitalMsgBol.Instance.LightStrip4_Bol = true;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        else
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LightStrip4_end);
                            DigitalMsgBol.Instance.LightStrip4_Bol = false;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        break;
                    case "LightStrip5":
                        if (MsgBol)
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LightStrip5_start);
                            DigitalMsgBol.Instance.LightStrip5_Bol = true;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        else
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LightStrip5_end);
                            DigitalMsgBol.Instance.LightStrip5_Bol = false;
                            Thread.Sleep(100);
                            AllBol = true;
                        }
                        break;
                    case "Transfer_Pump":
                        if (MsgBol)
                        {
                            //ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.Transfer_Pump_start);
                            //DigitalMsgBol.Instance.Transfer_Pump_Bol = true;
                        }
                        else
                        {
                            //ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.Transfer_Pump_end);
                            //DigitalMsgBol.Instance.Transfer_Pump_Bol = false;
                        }
                        break;
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        #endregion
        #region 咖啡机websocket
        public WSocketClientHelp wSocketClient = new WSocketClientHelp("wss://" + IPorPortInfo.Instance.CoffeeIP + ":" + IPorPortInfo.Instance.CoffeePort);
        public void websocket()
        {
            try
            {
                wSocketClient.OnOpen += WSocketClient_OnOpen;
                wSocketClient.OnMessage += WSocketClient_OnMessage;
                wSocketClient.OnClose += WSocketClient_OnClose;
                wSocketClient.OnError += WSocketClient_OnError;
                wSocketClient.Open();
                isCoffeeSocket = true;
                //var dd = BaseService.Instance.ServerGet("device/status");
                //var json = JsonConvert.DeserializeObject<CoffeeMassage>(dd);//Ready
                //if (json != null)
                //    if (json.status == "Ready")
                //        isMaking = true;
                Thread.Sleep(100);
            }
            catch (Exception ex)
            {
                Thread.Sleep(100);
                wSocketClient.Close();
                wSocketClient.OnOpen -= WSocketClient_OnOpen;
                wSocketClient.OnMessage -= WSocketClient_OnMessage;
                wSocketClient.OnClose -= WSocketClient_OnClose;
                wSocketClient.OnError -= WSocketClient_OnError;
                websocket();
                return;
            }
        }
        private void WSocketClient_OnError(object sender, Exception ex)
        {

        }

        private void WSocketClient_OnClose(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 咖啡机状态变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="data"></param>
        private void WSocketClient_OnMessage(object sender, string data)
        {
            //处理的消息错误将会忽略
            try
            {
                var heart = JsonConvert.DeserializeObject<xintiao>(data);
                if (heart == null)
                {
                    var json = JsonConvert.DeserializeObject<CoffeeMassage>(data);//Ready
                    Console.WriteLine(json);
                    if (json.status == "Ready")
                        isMaking = true;
                    else
                        isMaking = false;
                }
                else
                {
                    Form1.pSocketClient.Send(JsonConvert.SerializeObject(new PrinterSel { code = 1, tag = 0 }));
                }
            }
            catch (Exception ex)
            {
                return;
            }

        }

        private void WSocketClient_OnOpen(object sender, EventArgs e)
        {

        }
        #endregion
        #region 打印机websocket
        /// <summary>
        /// 图片转base64
        /// </summary>
        /// <param name="path">图片地址</param>
        /// <returns></returns>
        public static string SetImg(string path)
        {
            return "image/jpeg;base64," + Convert.ToBase64String(System.IO.File.ReadAllBytes(path));
        }
        public static PrinterWebSocket pSocketClient = new PrinterWebSocket("ws://" + IPorPortInfo.Instance.PrinterCoffeeIP + ":" + IPorPortInfo.Instance.PrinterCofferPort);
        public static void Pwebsocket()
        {
            try
            {
                pSocketClient.OnOpen -= PSocketClient_OnOpen;
                pSocketClient.OnMessage -= PSocketClient_OnMessage;
                pSocketClient.OnClose -= PSocketClient_OnClose;
                pSocketClient.OnError -= PSocketClient_OnError;
                pSocketClient.OnOpen += PSocketClient_OnOpen;
                pSocketClient.OnMessage += PSocketClient_OnMessage;
                pSocketClient.OnClose += PSocketClient_OnClose;
                pSocketClient.OnError += PSocketClient_OnError;
                pSocketClient.Open();
                Thread.Sleep(1000);
                isPrinterSocket = true;
            }
            catch (Exception ex)
            {
                pSocketClient.Close();
                pSocketClient.OnOpen -= PSocketClient_OnOpen;
                pSocketClient.OnMessage -= PSocketClient_OnMessage;
                pSocketClient.OnClose -= PSocketClient_OnClose;
                pSocketClient.OnError -= PSocketClient_OnError;
                Pwebsocket();
                return;
            }
        }
        private static void PSocketClient_OnError(object sender, Exception ex)
        {

        }

        private static void PSocketClient_OnClose(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 打印机状态变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="data"></param>
        private static void PSocketClient_OnMessage(object sender, string data)
        {
            //处理的消息错误将会忽略
            try
            {
                Console.WriteLine(data + "123123132");
                var json = JsonConvert.DeserializeObject<PrinterMessageModel>(data);//Ready
                Console.WriteLine(data);
                if (json != null)
                {
                    switch (json.code)
                    {
                        //查询机器状态
                        case 1:
                            IsReviecePrinter = true;
                            switch (json.tag)
                            {
                                case 0:
                                    Form1.pSocketClient.Send(JsonConvert.SerializeObject(new PrinterSel { code = 1, tag = 0 }));
                                    break;
                                case 1:
                                case 2:
                                    isPrinter = true;
                                    break;
                                case 3:
                                    break;
                                case 4:
                                    break;
                                case 5:
                                    break;
                                default:
                                    break;
                            }
                            break;
                        case 2:
                            isPrinter = false;
                            switch (json.tag)
                            {
                                case 1:
                                case 4:
                                case 5:
                                case 6:
                                case 7:
                                    ErrorMsg = "ok";
                                    break;
                                case 2:
                                    ErrorMsg = "ok1"; ///"杯子尺寸使用不正确";
                                    break;
                                case 3:
                                    ErrorMsg = ""; //"图片处理数据失败";
                                    break;
                                case 11:
                                    ErrorMsg = "ok1";// "打印失败，托盘卡住";
                                    break;
                                case 8:
                                    ErrorMsg = "over";
                                    var Printer_Sel = new PrinterSel { code = 1, tag = 1 };
                                    pSocketClient.Send(JsonConvert.SerializeObject(Printer_Sel));
                                    break;
                                case 9:
                                    ErrorMsg = "ok1";// "打印失败，打印超时";
                                    break;
                                case 10:
                                    ErrorMsg = "ok1";// "打印失败，没有检测到杯子";
                                    break;
                                default:
                                    break;
                            }
                            break;
                        default:
                            break;
                            //case 3:
                            //    var Msg = JsonConvert.DeserializeObject<feedBackMsg>(json.data.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(data + "123123132789798");
                return;
            }

        }

        private static void PSocketClient_OnOpen(object sender, EventArgs e)
        {

        }
        #endregion
        #region 咖啡制作流程
        public static void QK()
        {
            try
            {
                AllBol = false;
                Thread.Sleep(100);
                ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_Start_Down);
                Thread.Sleep(100);
                ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QB1_Down);
                Thread.Sleep(100);
                ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QB2_Down);
                Thread.Sleep(100);
                ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_BJC_Down);
                Thread.Sleep(100);
                ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JKF_Down);
                Thread.Sleep(100);
                ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_KFWC_Down);
                Thread.Sleep(100);
                ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DY_Down);
                Thread.Sleep(100);
                ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DYWC_Down);
                Thread.Sleep(100);
                ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_CB_Down);
                Thread.Sleep(100);
                ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_SFDY_Down);
                Thread.Sleep(100);
                ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_SFDYNN_Down);
                Thread.Sleep(100);
                AllBol = true;
            }
            catch (Exception)
            {
                return;
            }
        }
        public static void QW()
        {
            AllBol = false;
            Thread.Sleep(100);
            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LOpenBulkPowerDown);
            Thread.Sleep(100);
            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_PTBPE_Down);
            Thread.Sleep(100);
            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_XSN_Down);
            Thread.Sleep(100);
            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LCloseBulkPowerDown);
            Thread.Sleep(100);
            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QDCX_Down);
            Thread.Sleep(100);
            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_ZTCX_Down);
            Thread.Sleep(100);
            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JXYXCX_Down);
            Thread.Sleep(100);
            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_Stop_Down);
            Thread.Sleep(100);
            AllBol = true;
        }
        public void CoffeeMakingList(string makingID)
        {
            try
            {
                if (coffeeMakeLists == null)
                    coffeeMakeLists = new CoffeeMakeMsg();
                coffeeMakeLists.ID += 1;
                CoffeeMakeList makeList = new CoffeeMakeList();
                makeList.daycode = coffeeMakeLists.ID;
                makeList.MakingID = makingID;
                coffeeMakeLists.lists.Add(makeList);
                File.WriteAllText(coffeeMakeListsUrl, JsonConvert.SerializeObject(coffeeMakeLists));
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 全部订单查询
        /// </summary>
        public void MessageShow()
        {
            while (true)
            {
                try
                {
                    Device device = new Device();
                    string lists = JsonConvert.SerializeObject(device);
                    var data = HttpMsg.Instance.deviceOrderQueueListMsg(lists);

                    if (data != null)
                    {
                        if (isMake)
                        {
                            var msglist = JsonConvert.DeserializeObject<HTTPMsg>(data);
                            if (msglist != null)
                            {
                                if (msglist.data.ToString().Length > 10)
                                {
                                    var list = JsonConvert.DeserializeObject<List<deviceOrderQueueList>>(msglist.data.ToString());
                                    if (list.Count > 0)
                                    {
                                        if (isMake)
                                        {
                                            if (list.Count > 0)
                                            {
                                                var ddd = list.FirstOrDefault(a => a.typeList2.Count > 0);
                                                if (ddd == null) continue;
                                                // if (list[0].typeList2 == null) continue;
                                                if (ddd.typeList2.Count > 0)
                                                {
                                                    var make = ddd.typeList2.FirstOrDefault(a => a.status == "3" || a.status == "4");
                                                    if (make != null)
                                                    {
                                                        if (make.jsonCodeVal.Length > 4)
                                                        {
                                                            var coffeecode = JsonConvert.DeserializeObject<List<specification>>(make.jsonCodeVal);
                                                            if (coffeecode.Count > 0)
                                                            {
                                                                var coffeeID = coffeecode.FirstOrDefault(a => a.classCode != "");
                                                                if (coffeeID != null)
                                                                {
                                                                    RobotOperate.CX_Start();
                                                                    Console.WriteLine(DateTime.Now.ToString() + "  cx5");
                                                                    if (RobotOperate.pstatus == JKTYPE.ProgramState.PROGRAM_RUNNING)
                                                                    {
                                                                        if (isCup)
                                                                            // CoffeeMaking("1003", null);
                                                                            CoffeeMaking(coffeeID.classCode, make);
                                                                    }
                                                                }
                                                            }
                                                        }

                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    if (MakingTypes != 0)
                                    {
                                        isMake = false;
                                        CoffeeMakings(MakingTypes.ToString(), img_path);
                                    }
                                }
                            }

                        }
                    }
                    Thread.Sleep(1000);
                }
                catch (Exception)
                {
                    //return;
                    Thread.Sleep(1000);
                }
            }
        }
        public void CoffeeMaking(string makecoffeeName, DeviceOrderQueue list, bool CoffeeBol = true)
        {
            try
            {
                if (!containerCoffeeGroundsBol)
                {
                    isMake = true;
                    return;
                }
                if (!DiaitalBool.Instance.CoffeeBean_Sel_Bol || !DiaitalBool.Instance.Milk_Sel_Bol || !DiaitalBool.Instance.Water_Sel_Bol)
                {
                    isMake = true;
                    return;
                }
                if (!DiaitalBool.Instance.OutMealCup_Sel_Bol)
                {
                    isMake = true;
                    return;
                }
                isMake = false;
                EditDeviceOrderStatus editDeviceOrder = new EditDeviceOrderStatus();
                editDeviceOrder.status = 4;
                editDeviceOrder.orderId = list.orderId;
                editDeviceOrder.orderGoodsId = list.id;
                var ddd = JsonConvert.SerializeObject(editDeviceOrder);
                HttpMsg.Instance.editDeviceOrderStatusMsg(ddd);
                Thread.Sleep(1000);
                MakingType = "busy";
                if (CoffeeBol)
                {
                    AllBol = false;
                    Thread.Sleep(100);
                    ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DYD);
                    Thread.Sleep(100);
                    AllBol = true;
                    if (RobotBol)
                    {
                        QK();
                    }
                }
                if (CoffeeBol)
                {
                    AllBol = false;
                    Thread.Sleep(100);
                    ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_Start_UP);
                    Thread.Sleep(100);
                    ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QB1_UP);
                    Thread.Sleep(100);
                    AllBol = true;
                }
                Thread.Sleep(1000);
                CoffeeMakes.Instance.CoffeeMaking(makecoffeeName, list.lhImgPath);
                isCoffeeMake = true;
                AllBol = false;
                Thread.Sleep(100);
                //for (int i = 0; i < 10; i++)
                //{
                //    Thread.Sleep(1000);

                //}
                if (makecoffeeName == "2211" || makecoffeeName == "2212")
                {
                    RobotOperate.Robot_Move(7);
                    isCCup = true;
                    isCup = false;
                    RobotOperate.Robot_Move(15);
                    RobotOperate.Robot_Move(11);
                    RobotOperate.Robot_Move(17);
                }
                else
                {
                    AllBol = false;
                    Thread.Sleep(100);
                    RobotBol = false;
                    while (true)
                    {
                        Thread.Sleep(100);
                        ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_YCC);
                        Thread.Sleep(100);
                        if (RobotBol)
                        {
                            QK();
                            Thread.Sleep(200);
                            isCCup = true;
                            isCup = false;
                            break;
                        }
                    }
                    // AllBol = false;
                }
                AllBol = true;
                Thread.Sleep(200);
                if (makecoffeeName == "2211" || makecoffeeName == "2212")
                {
                    try
                    {
                        //AllBol = false;
                        //QK();
                        RobotOperate.Robot_Yun();
                        Thread.Sleep(1000);
                        while (true)
                        {
                            RobotOperate.CX_Start();
                            Console.WriteLine(DateTime.Now.ToString() + "  cx5");
                            if (RobotOperate.pstatus == JKTYPE.ProgramState.PROGRAM_RUNNING)
                            {
                                break;
                            }
                            Thread.Sleep(500);
                        }
                        //Thread.Sleep(100);
                        //ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QDCX_UP);
                        //Thread.Sleep(100);
                        //RobotBol = false;
                        //while (true)
                        //{
                        //    RobotOperate.CX_Start();
                        //    if (RobotOperate.pstatus == JKTYPE.ProgramState.PROGRAM_RUNNING)
                        //    {
                        //        QW();
                        //        break;
                        //    }
                        //    else
                        //    {
                        //        Thread.Sleep(2000);
                        //        ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QDCX_UP);
                        //        Thread.Sleep(500);
                        //    }
                        //}
                    }
                    catch (Exception)
                    {
                    }
                }
                if (makecoffeeName == "2211" || makecoffeeName == "2212")
                {
                    while (true)
                    {
                        if (!isCCup)
                        {
                            AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.CupWashingPump_start);
                            Thread.Sleep(4000);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.CupWashingPump_end);
                            Thread.Sleep(1000);
                            RobotOperate.Robot_Move(12);
                            AllBol = true;
                            break;
                        }
                        Thread.Sleep(100);
                    }
                }
                Thread.Sleep(1000);
                AllBol = true;
                Thread.Sleep(2000);
                GC.Collect();
                EditDeviceOrderStatus editDeviceOrder2 = new EditDeviceOrderStatus();
                editDeviceOrder2.status = 5;
                editDeviceOrder2.orderId = list.orderId;
                editDeviceOrder2.orderGoodsId = list.id;
                var ddd2 = JsonConvert.SerializeObject(editDeviceOrder2);
                HttpMsg.Instance.editDeviceOrderStatusMsg(ddd2);
                MakingType = "ready";
                isMake = true;

            }
            catch (Exception)
            {
                return;
            }
        }
        public static int MakingTypes = 0;
        public static string img_path = "";
        public void CoffeeMakings(string makecoffeeName, string imgInit)
        {
            try
            {
                if (!containerCoffeeGroundsBol)
                {
                    isMake = true;
                    return;
                }
                if (!DiaitalBool.Instance.CoffeeBean_Sel_Bol || !DiaitalBool.Instance.Milk_Sel_Bol || !DiaitalBool.Instance.Water_Sel_Bol)
                {
                    isMake = true;
                    return;
                }
                if (!DiaitalBool.Instance.OutMealCup_Sel_Bol)
                {
                    isMake = true;
                    return;
                }
                isMake = false;

                Thread.Sleep(1000);
                MakingType = "busy";

                AllBol = false;
                Thread.Sleep(100);
                ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DYD);
                Thread.Sleep(100);
                AllBol = true;
                if (RobotBol)
                {
                    QK();
                }
                AllBol = false;
                Thread.Sleep(100);
                ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_Start_UP);
                Thread.Sleep(100);
                ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QB1_UP);
                Thread.Sleep(100);
                AllBol = true;
                Thread.Sleep(1000);
                CoffeeMakes.Instance.CoffeeMaking(makecoffeeName, imgInit);
                isCoffeeMake = true;
                AllBol = false;
                Thread.Sleep(100);
                if (makecoffeeName == "2211" || makecoffeeName == "2212")
                {
                    RobotOperate.Robot_Move(7);
                    RobotOperate.Robot_Move(15);
                    RobotOperate.Robot_Move(11);
                    RobotOperate.Robot_Move(17);
                }
                else
                {
                    AllBol = false;
                    Thread.Sleep(100);
                    RobotBol = false;
                    while (true)
                    {
                        Thread.Sleep(100);
                        ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_CC);
                        Thread.Sleep(100);
                        if (RobotBol)
                        {
                            QK();
                            break;
                        }
                    }
                    AllBol = false;
                }
                AllBol = true;
                if (makecoffeeName == "2211" || makecoffeeName == "2212")
                {
                    try
                    {
                        AllBol = false;
                        QK();
                        RobotOperate.Robot_Yun();
                        Thread.Sleep(1000);
                        while (true)
                        {
                            RobotOperate.CX_Start();
                            Console.WriteLine(DateTime.Now.ToString() + "  cx5");
                            if (RobotOperate.pstatus == JKTYPE.ProgramState.PROGRAM_RUNNING)
                            {
                                break;
                            }
                            Thread.Sleep(500);
                        }
                        //Thread.Sleep(100);
                        //ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QDCX_UP);
                        //Thread.Sleep(100);
                        //RobotBol = false;
                        //while (true)
                        //{
                        //    RobotOperate.CX_Start();
                        //    if (RobotOperate.pstatus == JKTYPE.ProgramState.PROGRAM_RUNNING)
                        //    {
                        //        QW();
                        //        break;
                        //    }
                        //    else
                        //    {
                        //        Thread.Sleep(2000);
                        //        ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QDCX_UP);
                        //        Thread.Sleep(500);
                        //    }
                        //}
                    }
                    catch (Exception)
                    {
                    }
                }
                if (makecoffeeName == "2211" || makecoffeeName == "2212")
                {
                    AllBol = false;
                    Thread.Sleep(100);
                    ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.CupWashingPump_start);
                    Thread.Sleep(4000);
                    ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.CupWashingPump_end);
                    Thread.Sleep(1000);
                    RobotOperate.Robot_Move(12);
                    AllBol = true;
                }
                Thread.Sleep(1000);
                AllBol = true;
                Thread.Sleep(2000);
                GC.Collect();
                MakingTypes = 0;
                MakingType = "ready";
            }
            catch (Exception)
            {
                return;
            }
        }
        public void JC()
        {
            try
            {
                this.Invoke(new Action(() =>
                {
                    while (!DiaitalBool.Instance.OutMealCup_Sel_Bol)
                    {
                        Thread.Sleep(50);
                        Console.WriteLine("123213");
                    }
                    isMake = true;
                }));
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 出餐门流程
        /// </summary>
        public void MakeMealMsg()
        {
            try
            {
                AllBol = false;
                Thread.Sleep(100);
                ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_start);
                Thread.Sleep(100);
                AllBol = true;
                while (true)
                {
                    if (DiaitalBool.Instance.PushRodDown_Sel_Bol)
                    {
                        break;
                    }
                    else
                    {
                        Thread.Sleep(100);
                    }
                    Thread.Sleep(10);
                }
                AllBol = false;
                Thread.Sleep(100);
                ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_start);
                Thread.Sleep(100);
                ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_end);
                Thread.Sleep(200);
                ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.OutMealDoor_UP_start);
                Thread.Sleep(200);
                AllBol = true;
                while (true)
                {
                    if (DiaitalBool.Instance.OutMealDoorUP_Sel_Bol)
                    {
                        break;
                    }
                    else
                    {
                        Thread.Sleep(100);
                    }
                    Thread.Sleep(10);
                }
                AllBol = false;
                Thread.Sleep(200);
                ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.OutMealDoor_UP_end);
                Thread.Sleep(200);
                AllBol = true;
                try
                {
                    coffeeMakeLists.lists[0].MakingType = "complete";
                    if (clientSocketes != null)
                        clientSocketes.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(coffeeMakeLists.lists)));
                }
                catch (Exception)
                {

                }

                while (true)
                {
                    if (!DiaitalBool.Instance.OutMealCup_Sel_Bol)
                    {
                        break;
                    }
                    else
                    {
                        Thread.Sleep(100);
                    }
                    Thread.Sleep(10);
                }
                Thread.Sleep(3000);
                while (true)
                {
                    if (!DiaitalBool.Instance.BANNER_Sel_Bol)
                    {
                        break;
                    }
                    else
                    {
                        Thread.Sleep(100);
                    }
                    Thread.Sleep(10);
                }
                Console.WriteLine("yes1");
                OutMealClose();
                Console.WriteLine("yes11");
                AllBol = false;
                Thread.Sleep(300);
                ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.OutMealDoor_Down_end);
                Console.WriteLine("yes3");
                Thread.Sleep(200);
                ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_start);
                Console.WriteLine("yes3");
                Thread.Sleep(100);
                AllBol = true;
                while (true)
                {
                    if (DiaitalBool.Instance.PushRodUP_Sel_Bol)
                    {
                        break;
                    }
                    Thread.Sleep(10);
                }
                AllBol = false;
                Thread.Sleep(100);
                ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
                Console.WriteLine("yes3");
                Thread.Sleep(100);
                AllBol = true;
            }
            catch (Exception)
            {
                return;
            }
        }
        public void OutMealClose()
        {
            try
            {

                bool bol = false;
                Console.WriteLine("yes2");
                AllBol = false;
                Thread.Sleep(200);
                ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.OutMealDoor_Down_start);
                Console.WriteLine("yes3");
                Thread.Sleep(200);
                AllBol = true;
                while (true)
                {
                    if (!DiaitalBool.Instance.BANNER_Sel_Bol)
                    {

                        //Console.WriteLine("yes5");
                        if (DiaitalBool.Instance.OutMealDoorDown_Sel_Bol)
                        {
                            Console.WriteLine("yes7");
                            AllBol = false;
                            Thread.Sleep(500);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.OutMealDoor_Down_end);
                            Thread.Sleep(200);
                            AllBol = true;
                            bol = false;
                            break;
                        }
                    }
                    else
                    {
                        AllBol = false;
                        Thread.Sleep(500);
                        ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.OutMealDoor_Down_end);
                        Thread.Sleep(200);
                        AllBol = true;
                        while (true)
                        {
                            if (DiaitalBool.Instance.OutMealDoorDown_Sel_Bol)
                            {
                                Console.WriteLine("yes9");
                                AllBol = false;
                                Thread.Sleep(500);
                                ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.OutMealDoor_Down_end);
                                Thread.Sleep(200);
                                AllBol = true;
                                break;
                            }
                            if (!DiaitalBool.Instance.BANNER_Sel_Bol)
                            {
                                bol = true;
                                break;
                            }
                        }
                        if (bol)
                        {
                            break;
                        }
                    }
                    Thread.Sleep(10);
                    //Thread.Sleep(2000);


                }
                if (bol)
                {
                    OutMealClose();
                    //break;
                }
            }
            catch (Exception)
            {
                Console.WriteLine("yesno");
                return;
            }
        }

        #endregion
        /// <summary>
        /// 咖啡制作
        /// </summary>
        public void CoffeeMakeType(coffeemodel.CoffeeFlowType maketype)
        {
            try
            {
                switch (maketype)
                {
                    case coffeemodel.CoffeeFlowType.fructoseAftericeAftercoffee:
                        break;
                    case coffeemodel.CoffeeFlowType.iceAftercoffee:
                        break;
                    case coffeemodel.CoffeeFlowType.fructoseAftercoffee:
                        break;
                    case coffeemodel.CoffeeFlowType.fructoseAftercoffeeAfterMilk:
                        break;
                    case coffeemodel.CoffeeFlowType.cooffeeAfterMilk:
                        coffeemake.CoffeeMakes.Instance.cooffeeAfterMilkMake(CoffeeProductIDs.espresso, CoffeeProductIDs.hotmilk);
                        break;
                    case coffeemodel.CoffeeFlowType.coffee:
                        coffeemake.CoffeeMakes.Instance.cooffeeAfterMilkMake(CoffeeProductIDs.espresso, CoffeeProductIDs.hotmilk);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// PING 通 IP
        /// </summary>
        /// <param name="IP"></param>
        /// <returns></returns>
        public bool PingIP(string IP)
        {
            try
            {
                if (IP.Split('.').Count() < 4) return false;
                Ping pingSender = new Ping();
                PingReply reply = pingSender.Send(IP, 120);//第一个参数为ip地址，第二个参数为ping的时间
                if (reply.Status == IPStatus.Success)
                {
                    return true;
                }
                else
                {
                    //ping不通
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                // RobotOperate.Robot_Move(1);

                //Coffee_making();
                //Robot_Move(5);...
                //Milk_making_1(); //咖啡制作流程
                //Robot_Move(6);
                //Robot_servo_j_Tulip();
                //Robot_Move(3);
            }
            catch (Exception)
            {
                return;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                CupMsg1();
            }
            catch (Exception)
            {
                return;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                Form1.AllBol = true;
                Form1.pSocketClient.pbol = false;
                Form1.IsReviecePrinter = false;
                if (Form1.pSocketClient.State != WebSocketState.Open)
                {
                    Form1.pSocketClient.Close();
                    Form1.Pwebsocket();
                    Thread.Sleep(2000);
                }
                Form1.pSocketClient.Send(JsonConvert.SerializeObject(new PrinterSel { code = 1, tag = 1 }));
                Thread.Sleep(500);
                int sel = 0;
                bool ispbol = true;
                while (!Form1.IsReviecePrinter)
                {
                    sel++;
                    Thread.Sleep(500);
                    if (sel == 20)
                    {
                        if (Form1.pSocketClient.State != WebSocketState.Open)
                        {
                            Form1.pSocketClient.Close();
                            Form1.Pwebsocket();
                            Thread.Sleep(2000);
                            Form1.pSocketClient.Send(JsonConvert.SerializeObject(new PrinterSel { code = 1, tag = 1 }));
                            Thread.Sleep(500);
                        }
                    }
                    if (sel == 40)
                    {
                        ispbol = false;
                        Form1.AllBol = false;
                        Thread.Sleep(100);
                        ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DYWC_UP);
                        Thread.Sleep(100);
                        Form1.AllBol = true;
                        Form1.ErrorMsg = "";
                        break;
                    }
                }
                if (ispbol)
                {
                    if (Form1.isPrinter)
                    {
                        Form1.RobotBol = false;
                        Form1.ErrorMsg = "";
                        if (Form1.pSocketClient.State != WebSocketState.Open)
                        {
                            Form1.pSocketClient.Close();
                            Form1.Pwebsocket();
                            Thread.Sleep(2000);
                        }
                        var Printer_Set = new PrinterSel2 { code = 2, tag = 1, data = new PrinterData { size = 90, img = Form1.SetImg(Form1.DYHImg) } };
                        Form1.pSocketClient.Send(JsonConvert.SerializeObject(Printer_Set));
                        Form1.IsReviecePrinter = false;
                        Thread.Sleep(1000);
                        int imgindex = 0;
                        while (Form1.ErrorMsg != "over" && Form1.ErrorMsg != "ok1")
                        {
                            Thread.Sleep(1000);
                            imgindex++;
                            if (imgindex == 27)
                            {
                                if (Form1.ErrorMsg == "")
                                {
                                    Form1.pSocketClient.Close();
                                    Form1.ErrorMsg = "over";
                                    break;
                                }
                            }
                        }
                        if (Form1.ErrorMsg == "over")
                        {
                            Form1.AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DYWC_UP);
                            Thread.Sleep(100);
                            Form1.AllBol = true;
                            Form1.ErrorMsg = "";
                        }
                        Form1.pSocketClient.pbol = true;
                        Form1.RobotBol = true;
                    }
                }
            }
            catch (Exception)
            {
                return;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                //CoffeeMaking(comboBox1.Text, null);
            }
            catch (Exception)
            {
                return;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                RobotOperate.Robot_servo_j_Swan2cs();
            }
            catch (Exception)
            {

                return;
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                RobotOperate.Robot_servo_j_Swan2();
            }
            catch (Exception)
            {

                return;
            }
        }
    }
    public class CoffeeMakeList
    {
        public int daycode { get; set; } = 0;
        public string MakingID { get; set; } = "";
        public string MakingType { get; set; } = "waiting";//wating making complete
    }
    public class CoffeeMakeMsg
    {
        public List<CoffeeMakeList> lists { get; set; } = new List<CoffeeMakeList>();
        public int ID { get; set; } = 0;
        public string date { get; set; } = DateTime.Now.ToString("yyyyMMdd");
    }
    public class SocketListNew
    {
        public Thread thread { get; set; }
        public Socket socket1 { get; set; }
        public string IP { get; set; }

    }
}
